package com.glsc.ngateway.platform.service.itflow.flowstrategy.cmdb.deviceIn.taskPostHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.cmdb.CMDBPassParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.cmdb.DeviceInTaskFormDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.cmdb.domainDto.FlowFormDeviceInDto;
import com.glsc.ngateway.common.api.oaflow.feign.IFeignOaFlowService;
import com.glsc.ngateway.common.api.oaflow.resp.InterfaceResponse;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDeviceDetail;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.FlowPushOaLog;
import com.glsc.ngateway.common.base.domain.oracle.ecology.UfZcinfo;
import com.glsc.ngateway.common.base.enums.itflow.ITFlowTaskEnum;
import com.glsc.ngateway.common.base.repo.mysql.cmdb.CmdbDeviceDetailRepository;
import com.glsc.ngateway.common.base.repo.oracle.ecology.UfZcinfoRepository;
import com.glsc.ngateway.platform.service.itflow.flowstrategy.IItFlowMQHandler;
import com.glsc.ngateway.platform.service.itflow.flowstrategy.cmdb.deviceIn.DeviceInFlowStrategy;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class DIStep4SyncOaTaskPostHandlerAudit implements IDeviceInTaskPostHandlerAudit, IItFlowMQHandler {

    @Autowired
    DeviceInFlowStrategy deviceInFlowStrategy;
    @Autowired
    DIStepHandlerHelper handlerHelper;
    @Resource
    private IFeignOaFlowService feignOaFlowService;
    @Resource
    private CmdbDeviceDetailRepository deviceDetailRepository;
    @Resource
    private UfZcinfoRepository zcinfoRepository;

    @Override
    public void doHandle(String msg) throws UnsupportedEncodingException {

    }

    @Override
    public String getTaskDefinitionKey() {
        return ITFlowTaskEnum.CMDB_DEVICE_IN_FLOW_SYNC_OA_TASK.getTaskKey();
    }

    @SneakyThrows
    @Override
    public void endFromOaFlow(FlowPushOaLog log, String description) {
        //修改服务器编号
        InterfaceResponse requestData = JSON.parseObject(feignOaFlowService.getRequestData(log.getRequestId()), InterfaceResponse.class);
        List<Map<String, JSONObject>> data = (List<Map<String, JSONObject>>) requestData.getData();
        JSONObject flowInfos = data.get(0).get("flowInfos");
        List<JSONObject> detailList = (List<JSONObject>) flowInfos.get("detailMap");
        Map<String, String> sn2ZcbhMap = detailList.stream().collect(Collectors.toMap(f -> f.get("sn").toString(), f -> f.get("zcbh").toString(), (o1, o2) -> o1));
        Map<String, UfZcinfo> zcbmMap = zcinfoRepository.findAllById(sn2ZcbhMap.values()).stream().collect(Collectors.toMap(UfZcinfo::getId, o -> o));
        List<CmdbDeviceDetail> deviceDetails = deviceDetailRepository.findAllBySnIn(new ArrayList<>(sn2ZcbhMap.keySet()))
                .stream()
                .peek(d ->
                        Optional.ofNullable(zcbmMap.get(sn2ZcbhMap.get(d.getSn()))).ifPresent(ufzc -> {
                            d.setAssertNum(ufzc.getZcbm());
                            d.setAssertId(ufzc.getId());
                        }))
                .collect(Collectors.toList());
        deviceDetailRepository.saveAll(deviceDetails);
        //结束流程
        CMDBPassParamDto<FlowFormDeviceInDto, DeviceInTaskFormDto> param = new CMDBPassParamDto<>();
        param.setProcessId(log.getProcessId());
        param.setTaskId(log.getTaskId());
        param.setTaskDefinitionKey(log.getTaskKey());
//        param.setFlowableFormData();
        deviceInFlowStrategy.taskAuditPass(param, log.getCreator());
    }

    @Override
    public String getMqCodeKey() {
        return ITFlowTaskEnum.CMDB_DEVICE_IN_FLOW_SYNC_OA_TASK.getMqCodeKey();
    }

}
